import time
import json
import hashlib
import re
import random


#V3.2

# 读取json文件
with open("data.json", "r",encoding="utf-8") as f:
    data = json.load(f)
for user_name,user_data in data.items():
    pass
with open("liushui.json", "r",encoding="utf-8") as f:
    liushui = json.load(f)
for user_liushuidan,user_liushui in liushui.items():
    pass
with open("kh.json","r",encoding="utf-8") as f:
   kh = json.load(f)
for kh_kh,kh_user in kh.items():
    pass

def yz_userzw():
    if re.search(r'[^a-zA-Z0-9]', zhuce_user):
        return ("no")
    else:
        if zhuce_user in data:
            print ("账户已存在")
            zhuce()
        else:
            return ("yes")
def zhuce():
    try:
        global zhuce_user
        zhuce_name = input("请输入注册名称:")
        zhuce_user = input("请输入注册账户，不能是中文:")
        if yz_userzw() == "no":
            print ("不能有除了英文和数字以外的字符")
            zhuce()
        elif yz_userzw() == "yes":
            zhuce_mima = int(input("请输入注册密码:"))
            zhuce_mima = str(zhuce_mima)
            zhuce_mima_zijie = zhuce_mima.encode()
            zhuce_mima_md5 = hashlib.md5(zhuce_mima_zijie).hexdigest()
            kh_suiji = suiji()
            data[zhuce_user] = {"username":zhuce_user,"user":zhuce_name,"mima":zhuce_mima_md5,"yuan":0,"ka_hao":suiji()}
            liushui[zhuce_user] = {"操作名称":["注册"],
                                  "对象":[zhuce_name],
                                  "余额流动":["未流动"],
                                  "时间":[time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())]}
            kh[kh_suiji] = {"username":zhuce_user,"kh":kh_suiji}
            with open("data.json", "w", encoding="utf-8") as f:
                json.dump(data,f,indent=4)
            with open("liushui.json", "w", encoding="utf-8") as f:
                json.dump(liushui,f,indent=4)
            with open("kh.json", "w", encoding="utf-8") as f:
                json.dump(kh,f,indent=4)
            print ("注册成功")
    except ValueError:
        print ("错误!")
        zhuce()
def chushihua():
    data = {}
    with open("data.json", "w", encoding="utf-8") as f:
        json.dump(data,f,indent=4)
    liushui = {}
    with open("liushui.json", "w", encoding="utf-8") as f:
        json.dump(liushui,f,indent=4)
    kh = {}
    with open("kh.json", "w", encoding="utf-8") as f:
        json.dump(kh,f,indent=4)
    
    print ("初始化成功")

def suiji(length=9,lower_bound=0,upper_bound=9):
    # 生成随机的卡号
    sclb = [str(random.randint(lower_bound,upper_bound))for _ in range(length)]
    return ''.join(sclb)


if __name__ == '__main__':
    print ("请选择使用服务：1.注册 2.初始化")
    print ("如果未进行初始化，无法使用服务")
    xz = int(input("请输入："))
    if xz == 1:
        zhuce()
    elif xz == 2:
        chushihua()
    else:
        print ("输入错误")
        exit()